﻿<%
	
	Dim PageSize
	Dim PageIndex
	Dim TotalRow
	
	Dim parentID
	Dim parentName
	PageSize =16
	If Request.QueryString("page")<>"" Then
		PageIndex = Clng(Request.QueryString("page"))
	else
		PageIndex = 1
	end if
	
	If Request.QueryString("id")<>"" Then
		parentID = Clng(Request.QueryString("id"))
	else
		parentID =1
	end if

	sSQL = "select * from category where id = " & parentID
	set rs = DBLayer.GetRecordSet(sSQL)
	While not rs.EOF 
		parentName = rs("name").Value
	rs.MoveNext
	Wend
	set rs = Nothing
	if Request.QueryString("mode")="search" then
		'tim kiem
		sSQL = "Select count(c.id) from content as c inner join category as ca on c.categoryid = ca.id" & _
	     " where c.isdeleted =0 and (c.title like N'%"& Request.QueryString("query")& "%' or c.description like N'%" &Request.QueryString("query") & "%' )" & _
		 " and c.status =3 and c.startpublish <='"& beginTime &"' and c.endpublish >='" & endTime & "'" 
	elseIf Request.QueryString("mode")="all" Then
		'Lay tat ca cac tin
		sSQL = "Select count(c.id) from content as c inner join category as ca on c.categoryid = ca.id where c.isdeleted =0 " & _
		" and c.status =3 and c.startpublish <='"& beginTime &"' and c.endpublish >='" & endTime & "'" 
	else	
	   'truong hop xem danh sach tin cua 1 category
	   sSQL = "Select count(c.id) from content as c inner join category as ca on c.categoryid = ca.id" & _
	   " where c.isdeleted=0 and (c.categoryid = " & parentID & " or ca.id in (select id from category where parentid =" & parentID & "))" & _
	   " and c.status =3 and c.startpublish <='"& beginTime &"' and c.endpublish >='" & endTime & "'" 
	end if
	
'Response.Write("Count sql " & sSQL & "<br>")
	set rs = DBLayer.GetRecordSet(sSQL)
	While not rs.EOF 
	TotalRow = rs(0).Value
	rs.MoveNext
	Wend
	set rs = Nothing
	
	
	'Response.Write("total row " & TotalRow)
	Dim sSQLa
	Dim rsa
	if Request.QueryString("mode")="search" then
		 'tim kiem
		sSQLa = "Select Top " & PageSize & " T1.* from (" & _
		   "Select c.*,ca.name as categoryname,f.filename, ROW_NUMBER() OVER (ORDER BY c.updatetime DESC) AS Row from content as c inner join category as ca on c.categoryid = ca.id" & _
		   " inner join [file] as f on f.contentid = c.id" & _
		   " where c.isdeleted =0 and (c.title like N'%"& Request.QueryString("query")& "%' or c.description like N'%" &Request.QueryString("query") & "%' )" & _
		   " and c.status =3 and c.startpublish <='"& beginTime &"' and c.endpublish >='" & endTime & "'"  & _
	   " ) as T1 where Row>" & ((PageIndex - 1) * PageSize) & " and Row<=" & (PageIndex * PageSize) & " order by T1.updatetime DESC"
	elseIf Request.QueryString("mode")="all" Then
		 'Lay tat ca
		sSQLa = "Select Top " & PageSize & " T1.* from (" & _
		   "Select c.*,ca.name as categoryname,f.filename, ROW_NUMBER() OVER (ORDER BY c.updatetime DESC) AS Row from content as c inner join category as ca on c.categoryid = ca.id" & _
		   " inner join [file] as f on f.contentid = c.id" & _
		   " where c.isdeleted =0" & _
		   " and c.status =3 and c.startpublish <='"& beginTime &"' and c.endpublish >='" & endTime & "'" & _
	   " ) as T1 where Row>" & ((PageIndex - 1) * PageSize) & " and Row<=" & (PageIndex * PageSize) & " order by T1.updatetime DESC"
     else
	  'truong hop xem danh sach tin cua 1 category
	
	   sSQLa = "Select Top " & PageSize & " T1.* from (" & _
		   "Select c.*,ca.name as categoryname,f.filename, ROW_NUMBER() OVER (ORDER BY c.updatetime DESC) AS Row from content as c inner join category as ca on c.categoryid = ca.id" & _
		   " inner join [file] as f on f.contentid = c.id" & _
		   " where c.isdeleted =0 and (c.categoryid = " & parentID & " or ca.id in (select id from category where parentid =" & parentID & "))" & _
		   " and c.status =3 and c.startpublish <='"& beginTime &"' and c.endpublish >='" & endTime & "'"  & _
	   " ) as T1 where Row>" & ((PageIndex - 1) * PageSize) & " and Row<=" & (PageIndex * PageSize) & " order by T1.updatetime DESC"
	 end if
	'Response.Write("select sql :" & sSQLa)
	
	
	

%>

<table class="table_body" cellpadding="0" cellspacing="0">
    <tbody>
        <tr>
            <td class="left_col" valign="top">
                <!--#Include File = "Left.asp" -->
            </td>
            <td class="middle_col" valign="top">
				<% 
				Dim title
				if Request.QueryString("mode")="search" then
					title = "KẾT QUẢ TÌM KIẾM CHO TỪ KHÓA '" & Request.QueryString("query") & "'"
				elseIf Request.QueryString("mode")="all" Then
					title ="TẤT CẢ CÁC TIN"
				else	
				    title ="TẤT CẢ TIN " & parentname
				 
				end if
				%>
                <span  class="title_cat"><%Response.Write(title)%></span>
                <hr>
                <table width="100%" border="0" cellpadding="4" cellspacing="0">
                    <tbody>
						<%
						set rsa = DBLayer.GetRecordSet(sSQLa)
						
						While NOT rsa.EOF 
						%>						
							
							<tr>
								<td>
									<b><a  href="<%Response.Write("Default.asp?act=content&id=" & rsa("id").Value )%>">
										<%Response.Write(rsa("title").Value)%></a></b><sup class="date"><%Response.Write(rsa("updatetime").Value)%></sup>
									<%
									if datevalue(CDate(rsa("updatetime").Value)) = datevalue(Cdate(now())) then
									%>									
									<img  src="js-css/icon_new1.jpg" style="border-width: 0px;"/>
									<%
									end if
									%>
									
								</td>
							</tr>
							<tr>
								<td><%If rsa("filename").Value <> "" Then %>
									<img src="/uploads/content/<%Response.Write(rsa("filename").Value)%>" alt="" width="100" align="left" hspace="3">
									<%End if%>
									<%Response.Write(rsa("description").Value)%>
								</td>
							</tr>
							<tr>
								<td>
									<hr>
								</td>
							</tr>
						<%
						rsa.MoveNext
						Wend
						%>

                       
                    </tbody>
                </table>
                <table align="right">
                    <tbody>
                        <tr>
                            <td>
                                Trang:
                            </td>
                            <td>
                                <table style="height: 30px;" border="0">
                                    <tbody>
                                        <tr><td>&nbsp;</td>
											<%
											Dim TotalPage
											TotalPage = Int((TotalRow-1) / PageSize)
											If TotalPage <= 0 Then
											%>
												 <td class="pageon">
													<a href="Default.asp?act=category&id=<%Response.Write(parentid)%>&page=1&mode=<%Response.Write(Request.QueryString("mode"))%>&query=<%Response.Write(Request.QueryString("query"))%>">1</a>
												</td>
											<%
											Else
												Dim x 
												For x=0 to TotalPage 
											%>
													<td class="<%If (x+1)= PageIndex Then Response.Write("pageon") Else Response.Write("page") End If%>">
													<a href="Default.asp?act=category&id=<%Response.Write(parentid)%>&page=<%Response.Write(x+1)%>&mode=<%Response.Write(Request.QueryString("mode"))%>&query=<%Response.Write(Request.QueryString("query"))%>"><%Response.Write(x+1)%></a></td>
											<%	
												Next
												
											End If
											%>
											
											 
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
            <td class="right_col" valign="top">
               <!--#Include File = "Right.asp" -->
            </td>
        </tr>
    </tbody>
</table>
